package com.study.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.model.system.SysDept;
import com.study.system.mapper.SysDeptMapper;
import com.study.system.service.SysDeptService;
import com.study.system.utils.DeptHelper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {

    //获取全部部门并封装成树形结构
    @Override
    public List<SysDept> findNodes() {
        List<SysDept> depts = baseMapper.selectList(null);
        List<SysDept> tree = DeptHelper.tree(depts);
        return tree;
    }

    //根据id删除部门
    @Override
    public void deleteById(Long id) {
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id);
        SysDept dept = baseMapper.selectOne(wrapper);
        if (dept != null){
            return;
        }else{
            baseMapper.deleteById(id);
        }
    }
}
